package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tcompany;
import com.xy.manage.service.TcompanyService;



@Controller
public class TcompanyAction{
	private static final Logger log = LoggerFactory.getLogger(TcompanyAction.class);
	public TcompanyAction() {
	}
 
 
 	/**
	 * 场所信息新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcompanyAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tcompany", new Tcompany());
        setDict(model);
		return "tcompanyEdit";
	}

	/**
	 * 场所信息新增保存
	 * 
	 * @param tcompany
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tcompanySave.do")
	public String save(Tcompany tcompany, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tcompany, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tcompany = tcompanyService.save(tcompany);
		return "redirect:v_tcompanyList.do";
	}

	/**
	 * 场所信息删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tcompanyDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tcompanyService.remove(id);
			}
		}
		return "redirect:v_tcompanyList.do";
	}

	/**
	 * 场所信息编辑页面
	 * 
	 * @param infoid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcompanyEdit.do")
	public String edit(long infoid, HttpServletRequest request, ModelMap model) {
		Tcompany tcompany = tcompanyService.get(infoid);
		model.addAttribute("tcompany", tcompany);
      model.addAttribute("actionEdit", request.getParameter("request"));
        setDict(model);
		return "tcompanyEdit";
	}

	/**
	 * 场所信息详情
	 * 
	 * @param infoid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcompanyDetail.do")
	public String detail(long infoid, HttpServletRequest request, ModelMap model) {
		Tcompany tcompany = tcompanyService.get(infoid);
		model.addAttribute("tcompany", tcompany);
        setDict(model);
		return "tcompanyView";
	}

	/**
	 * 场所信息查询（分页）
	 * 
	 * @param tcompany
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcompanyList.do")
	public String listPage(Tcompany tcompany, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tcompanyService.getTcompanyByPage(toMap(tcompany),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tcompanyList";
	}

	/**
	 * 场所信息查询
	 * 
	 * @param tcompany
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcompanyList1.do")
	public String list(Tcompany tcompany, HttpServletRequest request, ModelMap model) {
		List<Tcompany> list = tcompanyService.getTcompany(toMap(tcompany));
		model.addAttribute("list", list);
        setDict(model);
		return "tcompanyList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tcompany
	 * @return
	 */	private Map<String,Object> toMap(Tcompany tcompany){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tcompany.getInfoid()!=null&&!tcompany.getInfoid().equals("")){
			map.put("Infoid", tcompany.getInfoid());
		}
		if(tcompany.getUsername()!=null&&!tcompany.getUsername().equals("")){
			map.put("Username", tcompany.getUsername());
		}
		if(tcompany.getDZXX()!=null&&!tcompany.getDZXX().equals("")){
			map.put("DZXX", tcompany.getDZXX());
		}
		if(tcompany.getSSX()!=null&&!tcompany.getSSX().equals("")){
			map.put("SSX", tcompany.getSSX());
		}
		if(tcompany.getSSSQ()!=null&&!tcompany.getSSSQ().equals("")){
			map.put("SSSQ", tcompany.getSSSQ());
		}
		if(tcompany.getJLX()!=null&&!tcompany.getJLX().equals("")){
			map.put("JLX", tcompany.getJLX());
		}
		if(tcompany.getMPHM()!=null&&!tcompany.getMPHM().equals("")){
			map.put("MPHM", tcompany.getMPHM());
		}
		if(tcompany.getXZ()!=null&&!tcompany.getXZ().equals("")){
			map.put("XZ", tcompany.getXZ());
		}
		if(tcompany.getQZ()!=null&&!tcompany.getQZ().equals("")){
			map.put("QZ", tcompany.getQZ());
		}
		if(tcompany.getJBXX()!=null&&!tcompany.getJBXX().equals("")){
			map.put("JBXX", tcompany.getJBXX());
		}
		if(tcompany.getDWMC()!=null&&!tcompany.getDWMC().equals("")){
			map.put("DWMC", tcompany.getDWMC());
		}
		if(tcompany.getDWLB()!=null&&!tcompany.getDWLB().equals("")){
			map.put("DWLB", tcompany.getDWLB());
		}
		if(tcompany.getZZJGDM()!=null&&!tcompany.getZZJGDM().equals("")){
			map.put("ZZJGDM", tcompany.getZZJGDM());
		}
		if(tcompany.getLXDH()!=null&&!tcompany.getLXDH().equals("")){
			map.put("LXDH", tcompany.getLXDH());
		}
		if(tcompany.getDWXZ()!=null&&!tcompany.getDWXZ().equals("")){
			map.put("DWXZ", tcompany.getDWXZ());
		}
		if(tcompany.getXQPCS()!=null&&!tcompany.getXQPCS().equals("")){
			map.put("XQPCS", tcompany.getXQPCS());
		}
		if(tcompany.getDWCLRQ()!=null&&!tcompany.getDWCLRQ().equals("")){
			map.put("DWCLRQ", tcompany.getDWCLRQ());
		}
		if(tcompany.getSJZGBM()!=null&&!tcompany.getSJZGBM().equals("")){
			map.put("SJZGBM", tcompany.getSJZGBM());
		}
		if(tcompany.getFRXM()!=null&&!tcompany.getFRXM().equals("")){
			map.put("FRXM", tcompany.getFRXM());
		}
		if(tcompany.getFRZJLX()!=null&&!tcompany.getFRZJLX().equals("")){
			map.put("FRZJLX", tcompany.getFRZJLX());
		}
		if(tcompany.getFRZJHM()!=null&&!tcompany.getFRZJHM().equals("")){
			map.put("FRZJHM", tcompany.getFRZJHM());
		}
		if(tcompany.getFRLXDH()!=null&&!tcompany.getFRLXDH().equals("")){
			map.put("FRLXDH", tcompany.getFRLXDH());
		}
		if(tcompany.getFRSJHM()!=null&&!tcompany.getFRSJHM().equals("")){
			map.put("FRSJHM", tcompany.getFRSJHM());
		}
		if(tcompany.getDWFZRXM()!=null&&!tcompany.getDWFZRXM().equals("")){
			map.put("DWFZRXM", tcompany.getDWFZRXM());
		}
		if(tcompany.getDWFZRZJZL()!=null&&!tcompany.getDWFZRZJZL().equals("")){
			map.put("DWFZRZJZL", tcompany.getDWFZRZJZL());
		}
		if(tcompany.getDWFZRZJHM()!=null&&!tcompany.getDWFZRZJHM().equals("")){
			map.put("DWFZRZJHM", tcompany.getDWFZRZJHM());
		}
		if(tcompany.getDWFZRLXDH()!=null&&!tcompany.getDWFZRLXDH().equals("")){
			map.put("DWFZRLXDH", tcompany.getDWFZRLXDH());
		}
		if(tcompany.getDWFZRSJHM()!=null&&!tcompany.getDWFZRSJHM().equals("")){
			map.put("DWFZRSJHM", tcompany.getDWFZRSJHM());
		}
		if(tcompany.getZAFZRXM()!=null&&!tcompany.getZAFZRXM().equals("")){
			map.put("ZAFZRXM", tcompany.getZAFZRXM());
		}
		if(tcompany.getZAFZRZJZL()!=null&&!tcompany.getZAFZRZJZL().equals("")){
			map.put("ZAFZRZJZL", tcompany.getZAFZRZJZL());
		}
		if(tcompany.getZAFZRZJHM()!=null&&!tcompany.getZAFZRZJHM().equals("")){
			map.put("ZAFZRZJHM", tcompany.getZAFZRZJHM());
		}
		if(tcompany.getZAFZRLXDH()!=null&&!tcompany.getZAFZRLXDH().equals("")){
			map.put("ZAFZRLXDH", tcompany.getZAFZRLXDH());
		}
		if(tcompany.getZAFZRSJHM()!=null&&!tcompany.getZAFZRSJHM().equals("")){
			map.put("ZAFZRSJHM", tcompany.getZAFZRSJHM());
		}
		if(tcompany.getXFFZRGLRXM()!=null&&!tcompany.getXFFZRGLRXM().equals("")){
			map.put("XFFZRGLRXM", tcompany.getXFFZRGLRXM());
		}
		if(tcompany.getXFFZRGLRLXDH()!=null&&!tcompany.getXFFZRGLRLXDH().equals("")){
			map.put("XFFZRGLRLXDH", tcompany.getXFFZRGLRLXDH());
		}
		if(tcompany.getXFAQZRR()!=null&&!tcompany.getXFAQZRR().equals("")){
			map.put("XFAQZRR", tcompany.getXFAQZRR());
		}
		if(tcompany.getXFZRRLXDH()!=null&&!tcompany.getXFZRRLXDH().equals("")){
			map.put("XFZRRLXDH", tcompany.getXFZRRLXDH());
		}
		if(tcompany.getQT()!=null&&!tcompany.getQT().equals("")){
			map.put("QT", tcompany.getQT());
		}
		if(tcompany.getCreatetime()!=null&&!tcompany.getCreatetime().equals("")){
			map.put("Createtime", tcompany.getCreatetime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("SSXMap", DictUtils.getDictByType(DictUtils.TCOMPANY_SSX));
      model.addAttribute("DWLBMap", DictUtils.getDictByType(DictUtils.TCOMPANY_DWLB));
      model.addAttribute("DWXZMap", DictUtils.getDictByType(DictUtils.TCOMPANY_DWXZ));
      model.addAttribute("XQPCSMap", DictUtils.getDictByType(DictUtils.TCOMPANY_XQPCS));
      model.addAttribute("DWFZRZJZLMap", DictUtils.getDictByType(DictUtils.TCOMPANY_DWFZRZJZL));
      model.addAttribute("ZAFZRZJZLMap", DictUtils.getDictByType(DictUtils.TCOMPANY_ZAFZRZJZL));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tcompany tcompany, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TcompanyService tcompanyService;   
}